package com.im.interceptor;
import com.im.pojo.User;
import com.im.util.Key;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginInter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean res=false;
        HttpSession session =request.getSession();
        User user=(User)session.getAttribute(Key.SESSION_USER);
        if(user==null){
            String cp=request.getContextPath();
            try {
                response.sendRedirect(cp+"/login.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            res= false;
        }else{
            res= true;
        }
        return res;

    }
}
